package day04.concurrent01;

/**
 * CyclicBarrier 栅栏
 */
public class Demo05 {
    private static int i = 0;
    public static void main(String[] args) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                    System.out.println("一号玩家加载完成");
                    synchronized (Demo05.class){
                        i++;
                        if (i < 3){
                            Demo05.class.wait();
                        }else{
                            Demo05.class.notifyAll();
                        }
                        System.out.println("一号玩家开始游戏！");
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                    System.out.println("二号玩家加载完成");
                    synchronized (Demo05.class){
                        i++;
                        if (i < 3){
                            Demo05.class.wait();
                        }else{
                            Demo05.class.notifyAll();
                        }
                        System.out.println("二号玩家开始游戏！");
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                    System.out.println("三号玩家加载完成");
                    synchronized (Demo05.class){
                        i++;
                        if (i < 3){
                            Demo05.class.wait();
                        }else{
                            Demo05.class.notifyAll();
                        }
                        System.out.println("三号玩家开始游戏！");
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
